export function numberToChinese(num) {
  let changeNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp) => {
    let strArr = temp.toString().split("").reverse();
    let newNum = "";
    let newArr = [];
    strArr.forEach((item, index) => {
      newArr.unshift(
        item === "0" ? changeNum[item] : changeNum[item] + unit[index]
      );
    });
    let numArr = [];
    newArr.forEach((m, n) => {
      if (m !== "零") numArr.push(n);
    });
    if (newArr.length > 1) {
      newArr.forEach((m, n) => {
        if (newArr[newArr.length - 1] === "零") {
          if (n <= numArr[numArr.length - 1]) {
            newNum += m;
          }
        } else {
          newNum += m;
        }
      });
    } else {
      newNum = newArr[0];
    }

    return newNum;
  };
  let overWan = Math.floor(num / 10000);
  let noWan = num % 10000;
  if (noWan.toString().length < 4) {
    noWan = "0" + noWan;
  }
  return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}

/**
 * 格式化commonSentence
 * @param {string} str
 */
export const transformCommonSentence = (string = "") => {
  // 如果包含、就以、分割

  return string
    .replace(/\r/g, "")
    .split(/[\n\s]/)
    .map((v) => ({
      key: randomString(),
      value: v.trim().replace(/\d+、/g, ""),
    }));
};

export const formatTableCommonSentence = (string) => {
  if (!string || string.trim() === "") {
    return [];
  }
  const res = transformCommonSentence(string);
  return res.map((v, i) => {
    return `${i + 1}、${v.value}`;
  });
};

/**
 * 生成随机字符串
 * @param {number} e
 * @returns
 */
export function randomString(e) {
  e = e || 32;
  let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
    a = t.length,
    n = "";
  for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
  return n;
}

export function formatTime(time) {
	let date = new Date(time);
	let Y = date.getFullYear() + '-';
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	let D = date.getDate()< 10 ? '0' +date.getDate() + ' ': date.getDate() + ' ';
	let h = date.getHours()< 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
	let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
	let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() ;
	return Y + M + D + h + m + s;
}